package com.nbsystem.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbsystem.entity.ElcInfotype;
import com.nbsystem.entity.ElcNotice;
import com.nbsystem.entity.SysUser;
import com.nbsystem.entity.ViewNotice;
import com.nbsystem.service.ElcInfotypeService;
import com.nbsystem.service.ElcNoticeService;
import com.nbsystem.util.Const;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * (ElcNotice)表控制层
 *
 * @author makejava
 * @since 2021-06-01 15:01:28
 */
@RestController
@RequestMapping("elcNotice")
public class ElcNoticeController {
    /**
     * 服务对象
     */
    @Resource
    private ElcNoticeService elcNoticeService;

    /**
     * 公告主页的展示 以及分页展示
     *
     *
     * @param viewNotice
     * @param rows
     * @param page
     * @return
     */
    @RequestMapping("list")
    public PageInfo<ViewNotice> queryAll(ViewNotice viewNotice, Integer rows, Integer page ,HttpServletRequest request) {

        HttpSession session = request.getSession();
        SysUser sysUser = (SysUser) session.getAttribute(Const.USERSESSION);
        viewNotice.setUserId(sysUser.getId());
        //分页组件
        PageHelper.startPage(page, rows);
        if (viewNotice.getTitle() != null && viewNotice.getTitle().length() !=0) {
            viewNotice.setTitle("%" + viewNotice.getTitle() + "%");
        }

//        if (viewNotice.getInfoTypeId() != null) {
//            viewNotice.setInfoTypeId(Integer.valueOf(viewNotice.getInfoTypeId()));
//        }
        //查询所有信息
        List<ViewNotice> list = elcNoticeService.queryAllByUserId(viewNotice);
        PageInfo<ViewNotice> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }



    /**
     * 通过ID查询公告
     * @param id
     * @return
     */
    @RequestMapping("selectOne")
    public ElcNotice queryNoticeById(@RequestBody Integer id){
        ElcNotice elcNotice=elcNoticeService.queryById(id);
        return elcNotice;
    }

    @RequestMapping("delete/{id}")
    public  String deleteNoctice(@PathVariable Integer id){
        elcNoticeService.deleteById(id);
        return "success";
    }


    @RequestMapping("deletes/{ids}")
    public String deleteSysUsers(@PathVariable Integer[] ids){
        elcNoticeService.deleteByIds(ids);
        return "success";
    }

    @RequestMapping("queryById/{id}")
    private void queryById(HttpSession session, @PathVariable Integer id){
        ElcNotice elcNotice = elcNoticeService.queryById(id);
        session.setAttribute("notice",elcNotice);
    }
    @RequestMapping("updateState")
    public String updateNotice(ElcNotice elcNotice, HttpServletRequest request) {
        elcNotice.setUpdateTime(new Date());
        elcNoticeService.update(elcNotice);
        return "redirect:"+request.getServletContext().getContextPath()+"/notice/index";
    }
}